<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('添加新闻')" />
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>
    <th:block th:include="include :: jquery-smartwizard-css"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: bootstrap-select-css"/>
    <th:block th:include="include :: summernote-css"/>

</head>
<script src="../static/js/xm-select.js" th:src="@{/js/xm-select.js}"></script>

<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-news-add">
        <div class="tab-content">
            <div class="form-group">
                <label class="col-sm-1 control-label">文章标题</label>
                <div class="col-sm-11">
                    <input name="name" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-6">
                    <label class="col-sm-2 control-label">关联药品</label>
                    <div class="col-sm-10">
                        <select class="form-control m-b select2-allow-clear"  name="drugId"  id="drugId"
                                th:with="type=${@commonService.drugKeyAndValue()}" data-placeholder="请选择药品" onchange="changeDrug()"
                                lay-search="">
                            <option value="">请选择药品</option>
                            <option th:each="dict : ${type}" th:text="${dict.vals}"
                                    th:value="${dict.keyName}"></option>
                        </select>
                    </div>
                </div>
                <div class="col-sm-6">
                    <label class="col-sm-2 control-label">选择疾病</label>
                    <div class="col-sm-10">
                        <div id="selectDemo"></div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-6">
                    <label class="col-sm-2 control-label">关键词</label>
                    <div class="col-sm-10">
                        <input name="keyword" class="form-control" type="text">
                    </div>
                </div>
                <div class="col-sm-6">
                    <label class="col-sm-2 control-label">作者来源</label>
                    <div class="col-sm-10">
                        <input name="authorSource" class="form-control" type="text">
                    </div>
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-6">
                    <label class="col-sm-2 control-label">文章分类</label>
                    <div class="col-sm-10">
                        <select name="type" class="form-control m-b" th:with="type=${@dict.getType('drug_type')}">
                            <option value="">请选择分类</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                        </select>
                    </div>
                </div>
                <div class="col-sm-6">
                    <label class="col-sm-2 control-label">标签</label>
                    <div class="col-sm-10">
                        <label class="radio-box"> <input type="radio"  name="label"  value="0" checked/> 不选择 </label>
                        <label class="radio-box"> <input type="radio"  name="label"  value="1" /> 头条 </label>
                        <label class="radio-box"> <input type="radio"  name="label"  value="2" /> 顶置 </label>
                        <label class="radio-box"> <input type="radio"  name="label"  value="3" /> 推荐 </label>
                        <label class="radio-box"> <input type="radio"  name="label"  value="4" /> 特推 </label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-1 control-label">描述</label>
                <div class="col-sm-11">
                    <textarea name="description" class="form-control" type="text"></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-12">
                    <label class="col-sm-1 control-label">主图</label>
                    <div class="col-sm-11">
                        <input type="hidden" name="url">
                        <div class="file-loading">
                            <input class="form-control file-upload" id="url" name="file" type="file">
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-12">
                    <label class="col-sm-1 control-label">内容</label>
                    <div class="col-sm-11">
                        <input type="hidden" class="form-control" required>
                        <script id="content" name="content" type="text/plain"></script>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-1 control-label">温馨提示</label>
                <div class="col-sm-11">
                    <textarea name="warmPrompt" class="form-control" type="text"></textarea>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-1 control-label">参考资料</label>
                <div class="col-sm-11">
                    <textarea name="referenceMaterial" class="form-control" type="text"></textarea>
                </div>
            </div>

        </div>
    </form>
</div>

<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<th:block th:include="include :: summernote-js"/>
<th:block th:include="include :: jquery-smartwizard-js"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: bootstrap-select-js"/>
<th:block th:include="include :: summernote-js"/>
<script src="/ueditor/ueditor.config.js" type="text/javascript" charSet="utf-8"></script>
<script src="/ueditor/ueditor.all.js" type="text/javascript" charSet="utf-8"></script>
<script src="/ueditor/lang/zh-cn/zh-cn.js" type="text/javascript" charSet="utf-8"></script>
<script th:inline="javascript">

    var selectData = [];
    var diseaseList = [];
    var backDis = [];
    var disarr = [];
    var alldis = [];

    var prefix = ctx + "ydz/news"
    $("#form-news-add").validate({
        focusCleanup: true
    });
    function remove(e) {
        $(e).parent().remove();
    }
    $(document).ready(function () {
        $(".select2-allow-clear").select2({
            allowClear: true,
            width: null
        });
        //加载疾病下拉
        $.ajax({
            url: "/api/common/selectNewDisease",
            type: "get",
            success: res => {
                alldis = res.data
                for (let i = 0; i < alldis.length; i++){
                    let val = {}
                    val.name = alldis[i].name
                    val.diseaseId = alldis[i].id
                    val.pid = alldis[i].pid
                    selectData.push(val)
                }
                initXmSelect()
            }
        })
        parent.$('body').find('.layui-layer-max:not(.layui-layer-maxmin)').click()

    })
    function initXmSelect(){
        xmSelect.render({
            el: '#selectDemo',
            checkbox: true,
            disabled:true,
            repeat: false,
            autoRow: true,
            filterable: true,
            prop: {name: 'name', value: 'diseaseId',pid:'pid'},
            initValue: backDis,//回显
            height: '200px',
            tips: '请选择',
            tree: {
                show: true,
                showFolderIcon: true,
                showLine: true,
                indent: 20,
                expandedKeys: [-3],
                strict: false,
            },
            data: function () {
                return selectData
            },
            on: function (data) {
                disarr = data.arr;//arr:  当前多选已选中的数据
            },
        });
    }

    function changeDrug() {
        backDis = [];
        $.ajax({
            url: "/ydz/drugDisease/list",
            type: "post",
            //当药品id被清空时，对应的疾病数组也应为空，默认传0查空数据
            data: {drugId:$("#drugId").val() == "" ? 0:$("#drugId").val()},
            success: res => {
                diseaseList = res.rows
                console.log(diseaseList)
                diseaseList.forEach(item=>{
                    backDis.push(item.diseaseId)
                })
                initXmSelect()
            }
        })
    }
    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-news-add').serialize());
        }
    }

    $(".file-upload").fileinput({
        uploadUrl: ctx + 'common/upload',
        maxFileCount: 1,
        autoReplace: true
    }).on('fileuploaded', function (event, data, previewId, index) {
        $("input[name='" + event.currentTarget.id + "']").val(data.response.fileName)
    }).on('fileremoved', function (event, id, index) {
        $("input[name='" + event.currentTarget.id + "']").val('')
    })

    //富文本
    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function (action) {
        if (action == 'uploadimage' || action == 'uploadscrawl') {
            return '/common/upload';
        } else {
            return this._bkGetActionUrl.call(this, action);
        }
    }

    var content = UE.getEditor('content', {
        autoHeightEnabled: false,//是否自动长高
        autoFloatEnabled: true,//是否保持toolbar的位置不动
        scaleEnabled: true,//是否可以拉伸长高
        initialFrameWidth: 'auto',//初始化编辑器宽度
        initialFrameHeight: 300,//初始化编辑器高度
    });
    content.ready(function () {
    })



</script>
</body>
</html>